package org.inspursc.s0517.health.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.inspursc.s0517.health.common.dto.LoginDTO;
import org.inspursc.s0517.health.common.dto.SysOrganizationDTO;
import org.inspursc.s0517.health.common.entity.SysUser;
import org.inspursc.s0517.health.common.service.SysOrganizationService;
import org.inspursc.s0517.health.common.util.LogUtil;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.util.SpringSecurityUtils;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@Api(value = "登录的Controller")
public class LoginController {

    @Autowired
    private UserDetailsService customUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SysOrganizationService organizationService;

    @Autowired
    private SessionRegistry sessionRegistry;

    @Autowired
    private LogUtil logUtil;

    @ApiOperation(value = "用户登陆")
    @PostMapping(value = "/login")
    public ResponseEntity<ResponseVO> login(HttpServletRequest request,
                                            @ApiParam(value = "用户名") String username,
                                            @ApiParam(value = "密码") String password) {
        HttpSession session = request.getSession();
        SysUser user = (SysUser) customUserService.loadUserByUsername(username);
        if (null != user) {
            if (passwordEncoder.matches(password, user.getPassword())) {
                SysOrganizationDTO organizationDTO = organizationService.findOneById(user.getOrganizationId());
                if(organizationDTO!=null){
                    SpringSecurityUtils.saveUserDetailsToContext(user, request);
                    session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext());
                    LoginDTO dto=new LoginDTO();
                    dto.setId(user.getId());
                    dto.setUsername(user.getUsername());
                    dto.setUserType(user.getUserType());
                    dto.setOrganizationId(user.getOrganizationId());
                    dto.setDomainName(organizationDTO.getDomainName());
                    dto.setOrganizationName(organizationDTO.getName());
                    dto.setLogo(organizationDTO.getLogo());
                    logUtil.createLog(request);
                    return ResponseUtil.success("登陆成功",dto);
                }else {
                    return ResponseUtil.error("登录失败! 账号配置不正确，请联系系统管理员",null);
                }
            }
            return ResponseUtil.error("登陆失败", false);
        }
        return ResponseUtil.error("登陆失败", false);
    }

    @ApiOperation(value = "检测是登录")
    @GetMapping(value = "/checkIsLogin")
    public ResponseEntity<ResponseVO> checkIsLogin(HttpServletRequest request) {
        Authentication authentication=null;
        if(request!=null) {
            authentication = SpringSecurityUtils.getAuthentication(request);
        }
        if (null == authentication) {
            return ResponseUtil.success("未登陆",false);
        }
        return ResponseUtil.success("已登录",true);
    }
}
